package com.kly.cashmall.popup;

import android.Manifest;
import android.content.Context;

import com.kly.cashmall.R;
import com.kly.cashmall.utils.system.PermissionUtils;
import com.yanzhenjie.permission.AndPermission;

import razerdp.basepopup.BasePopupWindow;

public class TakePhotoWindow extends BasePopupWindow {

  private OnTakeListener takeListener;

  public TakePhotoWindow(Context context) {
    super(context);
    setContentView(R.layout.popup_take_select_photo);
    initView();
  }

  private void initView() {
    findViewById(R.id.open_camera).setOnClickListener(v -> {
      if (AndPermission.hasPermissions(getContext(),Manifest.permission.CAMERA)){
        if (takeListener != null) {
          takeListener.openCamera();
        }
        dismiss();
      }else {
        PermissionUtils.requestPermission(getContext(), data -> {
          if (takeListener != null) {
            takeListener.openCamera();
          }
          dismiss();
        }, Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE);
      }
    });
    findViewById(R.id.open_gallery).setOnClickListener(v -> {
      if (takeListener != null) {
        takeListener.openGallery();
      }
      dismiss();
    });
    findViewById(R.id.cancel).setOnClickListener(v -> dismiss());
  }

  public void setTakeListener(OnTakeListener takeListener) {
    this.takeListener = takeListener;
  }

  public interface OnTakeListener {
    void openCamera();

    void openGallery();
  }
}
